﻿using Assets.Script.Data.EnemyData;
using Newtonsoft.Json;
using Script.EnemySystem.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using Script.UiSystem;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Video;

namespace Assets.Script.Human.Enemy
{
    /// <summary>
    ///  怪物生命周期
    /// </summary>
    public class EnemyAI : HumanInterface
    {
        /// <summary>
        /// 敌人net id
        /// </summary>
        [HideInInspector]
        public int id;

        [SerializeField] 
        protected EnemyBody body;

        protected EnemyActionManager actions;

        private int animIndex = 0;
        public int dieFrame;

        private void Awake()
        {
            actions = gameObject.AddComponent<EnemyActionManager>();
            
        }

        protected virtual void Start()
        {
            
        }

        private void Update()
        {
            AI();
        }

        /// <summary>
        /// 负责初始化,包括EnemyActionManager actions
        /// </summary>
        public virtual void Init(EnemyData enemyData, int netId, int dieFrame)
        {
            if (enemyData!=null)
            {
                body.Data = enemyData;
            }
            this.id = netId;
            this.dieFrame = dieFrame;
        }

        public void UpdateSyncData(BattleSystem.SyncModle.EnemySyncData_Body syncData)
        {
            
            if (animIndex!= syncData.animIndex)
            {
                animIndex = syncData.animIndex;
                //todo 播放对应动画
            }
            
            transform.position = syncData.spaceInfo.pos;
            if (syncData.atkInex!=-1)
            {
                actions.ChangeState(syncData.atkInex);
            }
        }

        protected virtual void AI()
        {

        }

        #region 受伤
        public override void TakeDamage(float dmg, float force, float forceTime, Vector3 position,int playerId=-1)
        {
            if (playerId == GlobalCache.PlayerNetId)
            {
                BattleController.Instance.SendAtkEnemy(id, dmg);
            }
            body.TakeDamage(dmg, force, position);
            HurtReaction_Effect(position, ref force, ref forceTime);
            if (body.nowHp<=0)
            {
                EventManager.Broadcast(EventName.Enemy.Die, playerId);//todo 敌人死亡广播敌人信息
                // Die();
            }
        }

        /// <summary>
        /// 掉落物品
        /// 改变状态机
        /// </summary>
        public virtual void Die()
        {
            DieEffect();
            //actions.ChangeState(EnemyStateEnum.Die, null);
            Drop();
        }

        protected virtual void Drop()
        {

        }

        /// <summary>
        /// 死亡特效
        /// </summary>
        protected virtual void DieEffect()
        {
            Destroy(gameObject);
        }

        private Vector3 GetOffSetDir(Vector3 positonDir,ref float force, ref float forceTime)
        {
            positonDir.z = 0;
            var v3 = transform.position;
            v3.z = 0;
            Vector3 backTo = v3 - positonDir;
            return backTo.normalized;
        } 
        
        /// <summary>
        /// 受伤特效
        /// </summary>
        /// <param name="positonDir">被攻击方向</param>
        /// <param name="force">击退力</param>
        /// <param name="forceTime">击退时间</param>
        protected virtual void HurtReaction_Effect(Vector3 positonDir, ref float force, ref float forceTime)
        {

        }
        #endregion



    }
}
